﻿
/// <reference path="../BaseObject.js" />
/// <reference path="_namespace.js" />


/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.drawing.Timer
/*****************************************************************************/

ajax.lang.addNamespace("ajax.drawing");


//-----------------------------------------------------------------------------
ajax.drawing.Timer = function()
{
    ajax.drawing.Timer.superConstructor.call(this);
    
    // Public Properties
    this.frames                  = 0;
    this.startTick               = 0;
    this.storedTick              = 0;
    this.lastTick                = 0;
	this.fps                     = 0;
	this.elapsedTime             = 0;
	
	// Call iterate now to protect clients from terribly big numbers for their first run.
	this.iterate();		
	
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.drawing.Timer", "ajax.BaseObject"); // Inherit
//-----------------------------------------------------------------------------

// For ms Ajax
ajax.drawing.Timer.prototype.frames                  = 0;
ajax.drawing.Timer.prototype.startTick               = 0;
ajax.drawing.Timer.prototype.storedTick              = 0;
ajax.drawing.Timer.prototype.lastTick                = 0;
ajax.drawing.Timer.prototype.fps                     = 0;
ajax.drawing.Timer.prototype.elapsedTime             = 0;
ajax.drawing.Timer.prototype.totalGameTime           = 0;

//-----------------------------------------------------------------------------
ajax.drawing.Timer.prototype.getTotalTime = function()
{ 
    var now = new Date();
    return now - this.startTick;   
}
//-----------------------------------------------------------------------------
ajax.drawing.Timer.prototype.toString = function()
{
    return "ajax.drawing.Timer";
}

//-----------------------------------------------------------------------------
ajax.drawing.Timer.prototype.iterate = function()
{			
	// Get the ticks right now
	var now = new Date();		
	var tickNow = now.getTime();	
	
	// Get the elapsed milliseconds since the last iteration
	this.elapsedTime            = tickNow - this.lastTick;	
	this.totalGameTime          += this.elapsedTime;
		
	this.lastTick    = tickNow;	
	

	// Work out the frames per second - ticks = milliseconds
	if (tickNow - this.storedTick > 1000)
	{
		// A second has elapsed - store how many frames we processed since the last second
		this.fps = this.frames;
		// Reset our ticker
		this.storedTick	= tickNow;
		this.frames		= 0;
	}
	// We are processing another frame
	this.frames++;
}	

//-----------------------------------------------------------------------------
ajax.drawing.Timer.prototype.getFpsString = function()
{
    return "FPS: " + this.fps.toString();
}

//-----------------------------------------------------------------------------
ajax.drawing.Timer.cast = function(obj)
{///<returns type="ajax.drawing.Timer"/>
    return obj;    
}
